<?php

namespace app\api\controller;

use app\services\ChatService;
use support\Request;

class ChatController
{
    public function chatList(Request $request)
    {
        try {
            $data = ChatService::chatList();

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function newChat(Request $request)
    {
        try {
            $exchangeId = $request->post('exchange_id');

            $id = ChatService::newChat($exchangeId);

            return ok_rsp('成功', ['chat_id' => $id]);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function addMessage(Request $request)
    {
        try {
            $chatId = $request->post('chat_id');
            $messageType = $request->post('message_type');
            $messageValue = $request->post('content');

            ChatService::addMessage($chatId, $messageType, $messageValue);

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function messageList(Request $request)
    {
        try {
            $chatId = $request->post('chat_id');
            $page = $request->post('page', 1);

            $list = ChatService::messageList($chatId, $page);

            return ok_rsp('成功', $list);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function addToBlackList(Request $request)
    {
        try {
            $userId = $request->post('user_id');

            ChatService::addToBlackList($userId);

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function isInBlackList(Request $request)
    {
        try {
            $userId = $request->post('user_id');

            $data = ChatService::isInBlackList(get_user_id(), $userId);

            return ok_rsp('成功', ['exists' => $data]);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function removeFromBlackList(Request $request)
    {
        try {
            $userId = $request->post('user_id');

            ChatService::removeFromBlackList($userId);

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }
}